package com.muyu.system.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoAddresses;
import com.muyu.system.domain.DanmoShoppingSpecification;
import com.muyu.system.mapper.DanmoShoppingSpecificationMapper;
import com.muyu.system.req.DanmoSpecificationDto;
import com.muyu.system.service.IDanmoShoppingSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品规格Service业务层处理
 * 
 * @author kunzong
 * @date 2024-12-02
 */
@Service
public class DanmoShoppingSpecificationServiceImpl implements IDanmoShoppingSpecificationService
{
    @Autowired
    private DanmoShoppingSpecificationMapper danmoShoppingSpecificationMapper;

    /**
     * 查询商品规格
     * 
     * @param id 商品规格主键
     * @return 商品规格
     */
    @Override
    public DanmoShoppingSpecification selectDanmoShoppingSpecificationById(Long id)
    {
        return danmoShoppingSpecificationMapper.selectDanmoShoppingSpecificationById(id);
    }

    /**
     * 查询商品规格列表
     * 
     * @param danmoSpecificationDto 商品规格展示对象
     * @return 商品规格
     */
    @Override
    public Result selectDanmoShoppingSpecificationList(DanmoSpecificationDto danmoSpecificationDto)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        DanmoShoppingSpecification danmoShoppingSpecification = new DanmoShoppingSpecification();
        danmoShoppingSpecification.setShoppingId(danmoSpecificationDto.getShoppingId());
        List<DanmoShoppingSpecification> danmoShoppingSpecifications = danmoShoppingSpecificationMapper.selectDanmoShoppingSpecificationList(danmoShoppingSpecification);
        if (danmoSpecificationDto.getAddressId() == null){
            DanmoAddresses danmoAddresses = danmoShoppingSpecificationMapper.selectDanmoDefaultAddress(loginUser.getUserid());
            if (danmoAddresses == null){
                DanmoAddresses danmoAddressesFirst = danmoShoppingSpecificationMapper.selectDanmoDefaultAddressFirst(loginUser.getUserid());
                danmoSpecificationDto.setDanmoAddresses(danmoAddressesFirst);
            }
            danmoSpecificationDto.setDanmoAddresses(danmoAddresses);
        }else {
            DanmoAddresses danmoAddresses = danmoShoppingSpecificationMapper.selectDanmoAddressByAddressIdAndUserId(danmoSpecificationDto.getAddressId(), loginUser.getUserid());
            if (danmoAddresses == null){
                return Result.error("该地址不存在");
            }
            danmoSpecificationDto.setDanmoAddresses(danmoAddresses);
        }
        danmoSpecificationDto.setDanmoShoppingSpecification(danmoShoppingSpecifications);
        return Result.success(danmoSpecificationDto);
    }

    /**
     * 新增商品规格
     * 
     * @param danmoShoppingSpecification 商品规格
     * @return 结果
     */
    @Override
    public int insertDanmoShoppingSpecification(DanmoShoppingSpecification danmoShoppingSpecification)
    {
        danmoShoppingSpecification.setCreateTime(DateUtils.getNowDate());
        return danmoShoppingSpecificationMapper.insertDanmoShoppingSpecification(danmoShoppingSpecification);
    }

    /**
     * 修改商品规格
     * 
     * @param danmoShoppingSpecification 商品规格
     * @return 结果
     */
    @Override
    public int updateDanmoShoppingSpecification(DanmoShoppingSpecification danmoShoppingSpecification)
    {
        danmoShoppingSpecification.setUpdateTime(DateUtils.getNowDate());
        return danmoShoppingSpecificationMapper.updateDanmoShoppingSpecification(danmoShoppingSpecification);
    }

    /**
     * 批量删除商品规格
     * 
     * @param ids 需要删除的商品规格主键
     * @return 结果
     */
    @Override
    public int deleteDanmoShoppingSpecificationByIds(Long[] ids)
    {
        return danmoShoppingSpecificationMapper.deleteDanmoShoppingSpecificationByIds(ids);
    }

    /**
     * 删除商品规格信息
     * 
     * @param id 商品规格主键
     * @return 结果
     */
    @Override
    public int deleteDanmoShoppingSpecificationById(Long id)
    {
        return danmoShoppingSpecificationMapper.deleteDanmoShoppingSpecificationById(id);
    }
}
